/**
 * This class is the controller for the main view for the application. It is specified as
 * the "controller" of the Main view class.
 *
 * TODO - Replace this content of this view to suite the needs of your application.
 */
Ext.define('OrderConvert.view.main.MainController', {
    extend: 'Ext.app.ViewController',

    alias: 'controller.main',


    requires: [
        "OrderConvert.controller.order.OrderController"
    ],

    onItemSelected: function (sender, record) {

    },

    editOrder: function (btn) {
        var orderwin = Ext.widget("orderwin");
        orderwin.show();
    },

    delete: function () {
        Ext.Msg.confirm('提示', '是否确定该操作？',
            function (choice) {
                if (choice === 'yes') {
                    var mainList = Ext.ComponentQuery.query("mainlist")[0];
                    var orderController = Ext.create("OrderConvert.controller.order.OrderController");
                    var records = mainList.getSelection();
                    if (records.length > 0) {
                        var record = records[0].data;

                        Ext.Ajax.request({
                            url: OrderConvertUrl.constant.serverUrl + 'order/delete',
                            params: {
                                id: record.id
                            },
                            success: function (response) {
                                var text = response.responseText;
                                if (text) {
                                    var json = Ext.decode(text);
                                    if (json.success) {
                                        Ext.Msg.alert("提示", '操作成功！');
                                        orderController.reloadGridData();
                                    } else {
                                        Ext.Msg.alert("提示", "操作失败！");
                                    }
                                }
                            }
                        });
                    } else {
                        Ext.Msg.alert("提示", "请选择订单！");
                    }
                }
            }
            );

    },

    watchPdf: function () {
        var mainList = Ext.ComponentQuery.query("mainlist")[0];
        var records = mainList.getSelection();
        if (records.length > 0) {
            var record = records[0];
            console.log(record.data.pdf_name);
            var pdfUrl = OrderConvertUrl.constant.serverUrl + "upload/" + record.data.pdf_name;
            var win = Ext.widget("watchorderwin");
            win.show();
            var success = new PDFObject({ url: pdfUrl }).embed("pdf");
        } else {
            Ext.Msg.alert("提示", "请选择订单！");
        }
    },

    exportXls: function (btn) {
        var mainList = Ext.ComponentQuery.query("mainlist")[0];
        var records = mainList.getSelection();
        if (records.length > 0) {
            var record = records[0];
            var fileId = record.data.isConvert;
            if (fileId) {
                var url = OrderConvertUrl.constant.sheetServer + "sheet/export?documentId=" + record.data.sheetId;
                window.open(url);
            } else {
                Ext.Msg.alert("提示", "该订单没有转换！");
            }
        } else {
            Ext.Msg.alert("提示", "请选择订单！");
        }
    },

    exportTmpXls: function (btn) {
        var mainList = Ext.ComponentQuery.query("mainlist")[0];
        var records = mainList.getSelection();
        if (records.length > 0) {
            var record = records[0];
            var fileId = record.data.isConvert;
            if (fileId) {
                Ext.Ajax.request({
                    url: OrderConvertUrl.constant.serverUrl + 'order/getDetail',
                    params: {
                        id: record.id
                    },
                    success: function (response) {
                        var text = response.responseText;
                        if (text) {
                            var json = Ext.decode(text);
                            var url = OrderConvertUrl.constant.sheetServer + "sheet/export?documentId=" + json.tmpFileId;
                            window.open(url);
                        }
                    }
                });


            } else {
                Ext.Msg.alert("提示", "该订单没有转换！");
            }
        } else {
            Ext.Msg.alert("提示", "请选择订单！");
        }
    },
    
    exportPdf : function(){
        var mainList = Ext.ComponentQuery.query("mainlist")[0];
        var records = mainList.getSelection();
        if (records.length > 0) {
            var record = records[0];
            var fileId = record.data.isConvert;
            if (fileId) {
                var url = OrderConvertUrl.constant.serverUrl + "order/getPdf?id=" + record.data.id;
                window.open(url);
            } else {
                Ext.Msg.alert("提示", "该订单没有转换！");
            }
        } else {
            Ext.Msg.alert("提示", "请选择订单！");
        }
    },

    openXls: function () {
        var me = this;
        var viewModel = me.getViewModel();
        var mainList = Ext.ComponentQuery.query("mainlist")[0];
        var records = mainList.getSelection();
        if (records.length > 0) {
            var record = records[0];
            var fileId = record.data.sheetId;
            if (fileId) {
                window.open(OrderConvertUrl.constant.sheetServer + "view?editFileId=" + fileId);
             //  OrderConvertUrl.constant.sheetId = fileId;
             //  viewModel.set("sheetId",fileId);
             //   var order = Ext.ComponentQuery.query("app-main panel[name=seeOrder]");
             //   var appMain = Ext.ComponentQuery.query("app-main")[0];
             //   appMain.setActiveTab(order[0]);
            } else {
                Ext.Msg.alert("提示", "该订单没有转换！");
            }
        } else {
            Ext.Msg.alert("提示", "请选择订单！");
        }
    },

    useRule : function (btn) {
        var me = this;
        var mainList = Ext.ComponentQuery.query("mainlist")[0];
        var records = mainList.getSelection();
        if (records.length > 0) {
            var record = records[0];
            var fileId = record.data.sheetId;
            if (fileId) {
                Ext.Msg.confirm('提示', '是否确定该操作？',
                    function (choice) {
                        if(choice == "yes"){
                            // var url = OrderConvertUrl.constant.sheetServer+'sheet/useRule?orderId='+record.data.id;
                            // window.open(url);
                            
                            Ext.Ajax.request({
                                url: OrderConvertUrl.constant.sheetServer+'sheet/useRule',
                                params: {
                                    orderId: record.data.id
                                },
                                success: function(response){
                                    layer.closeAll('loading');
                                    var text = response.responseText;
                                    Ext.Msg.alert("提示","转换已经在后台运行，稍后即可导出文件");
                                },
                                failure : function () {
                                    layer.closeAll('loading');
                                }
                            });
                        }
                    }
                )
            } else {
                Ext.Msg.alert("提示", "该订单没有转换！");
            }
        } else {
            Ext.Msg.alert("提示", "请选择订单！");
        }

    }
});
